class Profile < ActiveRecord::Base
    validates_presence_of :email
    validates_format_of :email, :with => /^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$/i,
                                :message => 'must contain an @ symbol, at least one period after the @, and one letter in each segment'

    def validate
      self.errors.add_to_base('This e-mail address already exists, please enter another one') if exists?(self.email)
    end

    protected
    def exists?(email_address)
      Profile.count(:conditions => ['email=?', email_address]) > 0
    end
end